using FytSoa.Domain.Sys;
using FytSoa.Sugar;
using Mapster;
using MapsterMapper;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Sys;

/// <summary>
/// 授权表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v1")]
public class SysAuthorityService : IApplicationService
{
    private readonly SugarRepository<SysAuthority> _thisRepository;
    public SysAuthorityService(SugarRepository<SysAuthority> thisRepository)
    {
        _thisRepository = thisRepository;
    }

    /// <summary>
    /// 根据角色获得权限
    /// </summary>
    /// <returns></returns>
    [HttpGet("{roleId}")]
    public async Task<List<SysAuthorityDto>> GetByRoleAsync(long roleId)
    {
        var list = await _thisRepository.GetListAsync(m => m.RoleId == roleId && m.Types == 3,
            m => m.MenuId);
        return list.Adapt<List<SysAuthorityDto>>();
    }

    /// <summary>
    /// 根据用户获得授权的角色
    /// </summary>
    /// <returns></returns>
    [HttpGet("{adminId}")]
    public async Task<List<string>> GetByAdminAsync(long adminId)
    {
        var adminDb = _thisRepository.ChangeRepository<SugarRepository<SysAdmin>>();
        var model = await adminDb.GetSingleAsync(m => m.Id == adminId);
        return model.RoleGroup;
    }

    /// <summary>
    /// 为用户授权多个角色
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public async Task<bool> AddRoleAsync(SysAuthorityAdminByRoleParam param)
    {
        var roleDb = _thisRepository.ChangeRepository<SugarRepository<SysRole>>();
        var roleList = await roleDb.GetListAsync();
        var roleStr = new List<List<string>>();
        await _thisRepository.DeleteAsync(m => m.AdminId == param.AdminId && m.Types == 2);
        var list = new List<SysAuthority>();
        foreach (var item in param.RoleArr)
        {
            var parentIdList = roleList.FirstOrDefault(m => m.Id == long.Parse(item))?.ParentIdList;
            if (parentIdList != null)
                roleStr.AddRange(new []{parentIdList});
            list.Add(new SysAuthority()
            {
                AdminId = param.AdminId,
                RoleId = long.Parse(item),
                Types = 2
            });
        }

        var adminDb = _thisRepository.ChangeRepository<SugarRepository<SysAdmin>>();
        await adminDb.UpdateAsync(m => new SysAdmin()
        {
            RoleGroup = param.RoleArr,
            RoleGroupParent = roleStr
        }, m => m.Id == param.AdminId);
        return await _thisRepository.InsertRangeAsync(list);
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(SysAuthorityParam model)
    {
        await _thisRepository.DeleteAsync(m => m.RoleId == model.RoleId && m.Types == 3);
        var list = model.Menus.Select(item => new SysAuthority()
            {
                RoleId = model.RoleId, MenuId = item.MenuId, Api = item.Api, Types = 3
            })
            .ToList();

        return await _thisRepository.InsertRangeAsync(list);
    }
}